package com.hlf.modules.wechat.service.impl;

import com.hlf.common.utils.wx.WxUserInfo;
import com.hlf.modules.wechat.mapper.CustomerMapper;
import com.hlf.modules.wechat.model.Customer;
import com.hlf.modules.wechat.service.CustomerService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/12/7.
 */

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public Customer queryByOpenId(String openid) {
        return customerMapper.selectByOpenId(openid);
    }

    @Override
    public void insert(WxUserInfo wxUserInfo, Customer customer) {
        String salt = RandomStringUtils.randomAlphanumeric(20);
        customer.setUseSex(Byte.parseByte(wxUserInfo.getSex()));
        customer.setOpenId(wxUserInfo.getOpenid());
        customer.setWechatNickname(wxUserInfo.getNickname());
        customer.setHeadimg(wxUserInfo.getHeadimgurl());
        customer.setCountry(wxUserInfo.getCountry());
        customer.setProvince(wxUserInfo.getProvince());
        customer.setCity(wxUserInfo.getCity());
        customer.setLanguage(wxUserInfo.getLanguage());
        customer.setSalt(salt);
        customerMapper.insertSelective(customer);

    }

    @Override
    public void updateByOpenId(WxUserInfo wxUserInfo, Customer customer) {
        String salt = RandomStringUtils.randomAlphanumeric(20);
        customer.setUseSex(Byte.parseByte(wxUserInfo.getSex()));
        customer.setOpenId(wxUserInfo.getOpenid());
        customer.setWechatNickname(wxUserInfo.getNickname());
        customer.setHeadimg(wxUserInfo.getHeadimgurl());
        customer.setCountry(wxUserInfo.getCountry());
        customer.setProvince(wxUserInfo.getProvince());
        customer.setCity(wxUserInfo.getCity());
        customer.setLanguage(wxUserInfo.getLanguage());
        customer.setSalt(salt);
        customerMapper.updateByPrimaryKeySelective(customer);
    }
}
